#pragma once

#include "spin_lock.hpp"

#include <functional>
#include <vector>
#include <thread>
#include <queue>


class Task {
public:
    virtual void run() = 0;
};

class ThreadPool {
public:
    static void WorkerThread(ThreadPool *master);
    //构造函数
    ThreadPool(size_t thread_count = 0);
    ~ThreadPool();

    //并行执行
    void parallelFor( size_t width, size_t height, const std::function<void(size_t, size_t)> &lambda);
    //等待
    void wait() const;

    void addTask(Task *task);
    Task *getTask();
private:
    std::atomic<int> alive;
    std::vector<std::thread> threads;
    std::atomic<int> pending_task_count;
    //名为tasks，类型为TAsk *指针的列表
    //声明了一个名为 tasks 的变量，它是一个 std::list 类型的对象，存储的是指向 Task 对象的指针
    std::queue<Task *> tasks;
    //锁
    SpinLock spin_lock;
};